#pragma once

#include <iostream>
#include <string>
#include <vector>
#include <cassert>
#include <unordered_map>
#include <fstream>
#include "../comm/log.hpp"
#include "../comm/util.hpp"

/*******************************************************
 * 根据题目链表文件，加载所有的题目信息到内存中
 * model模块主要用来和数据进行交互，对外提供访问数据的接口
 *******************************************************/
namespace ns_model
{
    using namespace ns_log;
    using namespace ns_util;

    struct question
    {
        std::string number; // 题目编号
        std::string name;   // 题目名称
        std::string star;   // 题目难度
        int cpu_limit;      // 时间限制
        int mem_limit;      // 空间限制
        std::string desc;   // 题目描述
        std::string header; // 交给用户的代码编辑区
        std::string tail;   // 用户提交代码的测试用例
    };

    const std::string question_list = "./questions/qusetions.list";
    class model
    {
    private:
        /*<题号:题目细节>*/
        /*<key,value>*/
        std::unordered_map<std::string, question> _questions;

    public:
        model()
        {
            assert(LoadQuestionslist(question_list));
        }
        ~model()
        {

        }

    public:
        bool LoadQuestionslist(const std::string &question_list)
        {
            /*加载配置文件：questions/question.list+题目编号文件*/
            std::ifstream in(question_list);
            if (!in.is_open())
            {
                LOG(FATAL) << "加载题库失败，请检查是否存在题库文件" << "\n";
                return false;
            }
            std::string line;
            while (getline(in, line))
            {
                std::vector<std::string> tokens;
                string_util::splitstring(line, &tokens, " ");
                if (tokens.size() != 5)
                {
                    LOG(WARNING) << "加载的部分题目失败，请检查文件格式" << "\n";
                    continue;
                }
                /*1 回文数 简单 1 30000*/
                question q;
                q.number = tokens[0];
                q.name = tokens[1];
                q.star = tokens[2];
                q.cpu_limit = atoi(tokens[3].c_str());
                q.mem_limit = atoi(tokens[4].c_str());

                std::string question_path = "./questions/";
                question_path += tokens[0];
                /*"./questions/1"*/
                std::string question_path_desc = question_path;
                question_path_desc += "/desc.txt";

                file_util::read_file(question_path_desc, &q.desc, true);
                std::string question_path_header = question_path;
                question_path_header += "/header.cpp";
                file_util::read_file(question_path_header, &q.header, true);
                std::string question_path_tail = question_path;
                question_path_tail += "/tail.cpp";
                file_util::read_file(question_path_tail, &q.tail, true);

                _questions.insert({q.number, q});
            }
            LOG(INFO) << "题库加载成功..." << "\n";
            in.close();
            return true;
        }
    public:
        bool GetAllQuestions(std::vector<question> *out)
        {
            if (_questions.size() == 0)
            {
                LOG(ERROR) << "用户获取题库失败" << "\n";
                return false;
            }
            for (auto &e : _questions)
            {
                out->push_back(e.second);
            }
            return true;
        }
        bool GetOneQuestion(const std::string &number, question *q)
        {
            std::unordered_map<std::string, question>::iterator p = _questions.find(number);
            if (p == _questions.end())
            {
                LOG(ERROR) << "用户获取指定题目失败" << "\n";
                return false;
            }
            *q = (*p).second;
            return true;
        }
    };
}